//
//  WBMainViewController.swift
//  weibo-swift
//
//  Created by carmne on 2019/1/3.
//  Copyright © 2019 lessismore. All rights reserved.
//

import UIKit


//主控制
class WBMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        setupChindControllers()

        // Do any additional setup after loading the view.
    }
    
}
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */
    //extension 类似于OC中的分类，在Swift 中还可以用来切分代码块
    //可以吧相近功能的函数，放在一个extension 中
    //便于代码维护
    //注意： 和OC的分类一样,extension中不能定义属性
    //MARK:-设置界面
extension WBMainViewController
    {
        //设置所有子控制器
        private func setupChindControllers()
        {
            let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            let jsonPath = (docDir as NSString).appendingPathComponent("main.json")
            var data = NSData(contentsOfFile:jsonPath)
            
            if data == nil {
                let path = Bundle.main.path(forResource: "main.json", ofType: nil)
                data = NSData(contentsOfFile: path!)
            }
            
            guard let array = try? JSONSerialization.jsonObject(with: data! as Data, options: []) as? [[String: Any]] else {
                return
            }
            var arrayM = [UIViewController]()
            
            for dict in array!{
                arrayM.append(controller(dict: dict))
            }
            viewControllers = arrayM;
        }
        //1使用字典创建一个子控制器
        //-parmeter dict:信息字典 [clsName,title,imageName]
        //- rreturns:子控制器
   private func controller(dict: [String: Any]) -> UIViewController {
    
            //1.取得字典内容,let visitorDic = dict["visitorInfo"] as? [String: String]
            guard let clsName = dict["clsName"] as? String,
                let title = dict["title"] as? String,
                let imageName = dict["imageName"] as? String,
                let cls = NSClassFromString(Bundle.main.namespace + "." + clsName) as? WBBaseViewController.Type,
                let visitorDic = dict["visitorInfo"] as? [String: String]
                else {
                    return UIViewController()
            }
            //创建视图控制器
            let vc = cls.init();
            vc.title = title;
            vc.visitorInfoDict = visitorDic
             print("tabbar_"+imageName)
            vc.tabBarItem.image = UIImage(named: "tabbar_"+imageName)
    
            vc.tabBarItem.setTitleTextAttributes([NSAttributedStringKey.foregroundColor : UIColor.orange], for: .highlighted)
            vc.tabBarItem.setTitleTextAttributes([NSAttributedStringKey.font : UIFont.systemFont(ofSize: 12)], for: .normal)
      let nav = WBNavigationCotroller(rootViewController: vc)
            return nav
            
    
            
         
            
            
    }
        
        
    }


